package com.mjk.manager.satoken.starter.interceptor;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.stp.StpUtil;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * saToken 拦截器功能实现
 */
public class SaAuthCheck {


    public static boolean saCheck(Object work, Method method, Map<String, Object> params) {
        //是否已经登入
        if (isSaAnnotation(method)) {
            if (!StpUtil.isLogin()) {//没有登入
                return false;
            }
            // 是否需要登入权限检测
            SaCheckPermission permission = method.getAnnotation(SaCheckPermission.class);
            if (null != permission) {
                StpUtil.checkPermissionAnd(permission.value());
            }
        }
        return true;
    }


    /**
     * 判断是否需要进行登入认证
     *
     * @param method
     * @return
     */
    private static boolean isSaAnnotation(Method method) {
        SaCheckLogin checkLogin = method.getAnnotation(SaCheckLogin.class);
        SaCheckPermission permission = method.getAnnotation(SaCheckPermission.class);
        if (null != checkLogin || permission != checkLogin) {
            return true;
        }
        return false;
    }

}
